﻿project(quality_inspection_tool VERSION 1.0.7)

# project version
configure_file(${PROJECT_SOURCE_DIR}/version.h.in ${PROJECT_BINARY_DIR}/version.h @ONLY)
MESSAGE("generating project [ " ${PROJECT_NAME} " ]")
MESSAGE("version: " ${PROJECT_VERSION})

# zlbase设置
set(ZLBASE_Dir ${CMAKE_SOURCE_DIR}/common/third_party/zlbase-2.0.7)
include_directories(${ZLBASE_Dir}/include)
link_directories(${ZLBASE_Dir}/lib)

# qt5 setttings
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
set(Qt5_DIR "C:\\Qt\\Qt5.10.1\\5.10.1\\mingw53_32\\lib\\cmake\\Qt5")
find_package(Qt5 COMPONENTS Core Widgets Concurrent REQUIRED LinguistTools)
include_directories(
    ${Qt5Widgets_INCLUDE_DIRS}
)

# translation
set(TS_NAME qit)
set(TS_FILES ${PROJECT_SOURCE_DIR}/assets/ts/${TS_NAME}.ts)
set_source_files_properties(${TS_FILES} PROPERTIES OUTPUT_LOCATION ${CMAKE_BINARY_DIR})
add_custom_command(
    OUTPUT ${TS_NAME}
    PRE_BUILD
    COMMAND ${Qt5_LUPDATE_EXECUTABLE} -target-language zh_CN ${PROJECT_SOURCE_DIR} -ts ${TS_FILES}
    COMMAND ${Qt5_LRELEASE_EXECUTABLE} ${TS_FILES} -qm ${CMAKE_BINARY_DIR}/${TS_NAME}_CN.qm
)

# add include path
include_directories(
    ${PROJECT_BINARY_DIR}
    ${PROJECT_SOURCE_DIR}
)

# Remove the console of gui program
if(CMAKE_BUILD_TYPE MATCHES "Rel")
    if(WIN32)
        if(MSVC)
            set_target_properties(${PROJECT_NAME} PROPERTIES
                WIN32_EXECUTABLE YES
                LINK_FLAGS "/ENTRY:mainCRTStartup"
            )
        elseif(CMAKE_COMPILER_IS_GNUCXX)
                SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mwindows") # Not tested
        else()
            message(SEND_ERROR "You are using an unsupported Windows compiler! (Not MSVC or GCC)")
        endif(MSVC)
    elseif(APPLE)
        set_target_properties(${PROJECT_NAME} PROPERTIES
                MACOSX_BUNDLE YES
        )
    elseif(UNIX)
        # Nothing special required
    else()
        message(SEND_ERROR "You are on an unsupported platform! (Not Win32, Mac OS X or Unix)")
    endif(WIN32)
endif()

set(RESQRC res.qrc)
aux_source_directory(. SRC)
aux_source_directory(core SRC)
aux_source_directory(view SRC)
aux_source_directory(view/component SRC)
aux_source_directory(view/component/tools SRC)
aux_source_directory(view/component/dashboard SRC)
aux_source_directory(view/custom SRC)
add_executable(${PROJECT_NAME} 
    ${TS_NAME} ${SRC} ${RESQRC} ${QM_FILES}
)
target_link_libraries(${PROJECT_NAME} 
    Qt5::Core Qt5::Widgets Qt5::Concurrent
    zlbase
)

# 安装
get_target_property(_qmake_executable Qt5::qmake IMPORTED_LOCATION)
get_filename_component(_qt_bin_dir "${_qmake_executable}" DIRECTORY)
find_program(DEPLOYQT_EXECUTABLE NAMES windeployqt macdeployqt HINTS "${_qt_bin_dir}")
file(GLOB COMMON_LIBS ${CMAKE_SOURCE_DIR}/common/bin/*.*)
install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION .)
install(FILES
    ${CMAKE_BINARY_DIR}/${TS_NAME}_CN.qm
    ${COMMON_LIBS}
    DESTINATION .
)
if(WIN32)
    install(CODE "execute_process(
        WORKING_DIRECTORY ${CMAKE_INSTALL_PREFIX}
        COMMAND ${DEPLOYQT_EXECUTABLE} ${PROJECT_NAME}.exe -release
    )")
endif()
